package com.fht.spring_boot_study.model.account.service.impl;

import com.fht.spring_boot_study.model.account.entity.Resource;
import com.fht.spring_boot_study.model.account.dao.ResourceDao;
import com.fht.spring_boot_study.model.account.dao.RoleResourceDao;
import com.fht.spring_boot_study.model.account.entity.RoleResource;
import com.fht.spring_boot_study.model.account.service.ResourceService;
import com.fht.spring_boot_study.model.common.entity.ResultEntity;
import com.fht.spring_boot_study.model.common.entity.SearchBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private RoleResourceDao roleResourceDao;

    @Override
    @Transactional
    public ResultEntity<Resource> insertResource(Resource resource) {
        resource.setCreateDate(LocalDateTime.now());
        resourceDao.insertResource(resource);
        if (resource.getRoles() != null) {
            resource.getRoles().stream()
                    .forEach(item -> {
                        roleResourceDao.insertRoleResource(new RoleResource(item.getId(), resource.getId()));
                    });
        }
        return new ResultEntity<Resource>(ResultEntity.ResultStatus.SUCCESS.code, "Insert success", resource);
    }

    @Override
    @Transactional
    public ResultEntity<Resource> updateResource(Resource resource) {
        resourceDao.updateResource(resource);
        roleResourceDao.deleteRoleResourceByResourceId(resource.getId());
        if (resource.getRoles() != null) {
            resource.getRoles().stream()
                    .forEach(item -> {roleResourceDao.insertRoleResource(new RoleResource(item.getId(), resource.getId()));});
        }
        return new ResultEntity<Resource>(ResultEntity.ResultStatus.SUCCESS.code, "Update success", resource);
    }

    @Override
    @Transactional
    public ResultEntity<Object> deleteResourceById(int id) {
        resourceDao.deleteResourceById(id);
        roleResourceDao.deleteRoleResourceByResourceId(id);
        return new ResultEntity<Object>(ResultEntity.ResultStatus.SUCCESS.code, "Delete success");
    }

    @Override
    public Resource getResourceById(int id) {
        return resourceDao.getResourceById(id);
    }

    @Override
    public List<Resource> getResourcesByRoleId(int roleId) {
        return resourceDao.getResourcesByRoleId(roleId);
    }

    @Override
    public PageInfo<Resource> getResourcesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<Resource>(Optional
                .ofNullable(resourceDao.getResourcesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }

}
